import { ref } from 'vue'
import { defineStore } from 'pinia'
import cookies from '@/utils/util.cookies'
import { ACCESS_TOKEN } from '@/config/token-const'
import { throwError } from '@/utils/util'
import { getInfo } from '@/api/modules/home.api'

export const useUserStore = defineStore('user', () => {
  const token = {
    get value() {
      return cookies.get(ACCESS_TOKEN)
    },
    set value(val) {
      cookies.set(ACCESS_TOKEN, val)
    },
  }
  const userInfo = ref(null)

  async function getUserInfo(code) {
    try {
      const res = await getInfo(code)
      userInfo.value = res
      return res
    } catch (e) {
      throwError('useUserStore/getUserInfo', e)
    }
  }

  return {
    userInfo,
    token,
    getUserInfo
  }
})
